package com.orange.core.pojo.enums;

import com.orange.core.pojo.entity.permission.Permission;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 后台管理权限
 */
@Getter
@AllArgsConstructor
public enum EPermission implements Serializable {

    SYSTEM(Permission.of(10000, "系统管理", "system")),

    ADMIN(Permission.of(10100, "员工管理", "system:admin")),
    ADMIN_LIST(Permission.of(10101, "列表", "system:admin:list")),
    ADMIN_ADD(Permission.of(10102, "新增员工", "system:admin:add")),
    ADMIN_EDIT(Permission.of(10103, "编辑", "system:admin:edit")),
    ADMIN_STATUS(Permission.of(10104, "启用/停用", "system:admin:status")),
    ADMIN_CHANGE_PASSWORD(Permission.of(10105, "修改密码", "system:admin:cp")),

    ROLE(Permission.of(10200, "角色管理", "system:role")),
    ROLE_LIST(Permission.of(10201, "列表", "system:role:list")),
    ROLE_ADD(Permission.of(10202, "添加", "system:role:add")),
    ROLE_EDIT(Permission.of(10203, "编辑", "system:role:edit")),
    ROLE_STATUS(Permission.of(10204, "启用/禁用", "system:role:status")),

    // 用户管理
    USER(Permission.of(10300, "用户管理", "user")),
    USER_LIST(Permission.of(10301, "列表", "user:page")),
    USER_UPDATE(Permission.of(10302, "编辑", "user:update")),
    USER_STATUS(Permission.of(10303, "启用/停用", "user:status")),
    USER_CHANGE(Permission.of(12304, "换卡", "user:change")),
    USER_UNBIND(Permission.of(12305, "解绑", "user:unbind")),

    // 商铺管理
    MERCHANT(Permission.of(10400, "商品管理", "merchant")),

    // 商品分类
    PRODUCT_CATEGORY(Permission.of(10500, "商品分类", "merchant:productCategory")),
    PRODUCT_CATEGORY_LIST(Permission.of(10501, "列表", "merchant:productCategory:page")),
    PRODUCT_CATEGORY_SAVE(Permission.of(10502, "新增", "merchant:productCategory:save")),
    PRODUCT_CATEGORY_UPDATE(Permission.of(10503, "编辑", "merchant:productCategory:update")),
    PRODUCT_CATEGORY_DELETE(Permission.of(10504, "删除", "merchant:productCategory:delete")),

    // 积分商品
    CREDIT_PRODUCT(Permission.of(10600, "积分商品", "merchant:creditProduct")),
    CREDIT_PRODUCT_LIST(Permission.of(10601, "列表", "merchant:creditProduct:page")),
    CREDIT_PRODUCT_SAVE(Permission.of(10602, "新增", "merchant:creditProduct:save")),
    CREDIT_PRODUCT_UPDATE(Permission.of(10603, "编辑", "merchant:creditProduct:update")),
    CREDIT_PRODUCT_SHELF(Permission.of(10604, "上下架", "merchant:creditProduct:shelf")),
    CREDIT_PRODUCT_STOCK(Permission.of(10605, "修改库存", "merchant:creditProduct:stock")),
    CREDIT_PRODUCT_DELETE(Permission.of(10606, "删除", "merchant:creditProduct:delete")),

    // 积分订单管理
    CREDIT_ORDER(Permission.of(10700, "积分订单管理", "merchant:creditOrder")),
    CREDIT_ORDER_LIST(Permission.of(10701, "列表", "merchant:creditOrder:page")),
    CREDIT_ORDER_REFUND_AUDIT(Permission.of(10702, "退款审核", "merchant:creditOrder:refundAudit")),

    // 门店管理
    SHOP(Permission.of(10800, "门店管理", "shop")),

    // 品牌商铺分类
    SHOP_CATEGORY(Permission.of(10900, "品牌店铺分类", "shop:shopCategory")),
    SHOP_CATEGORY_LIST(Permission.of(10901, "列表", "shop:shopCategory:page")),
    SHOP_CATEGORY_INFO(Permission.of(10902, "详情", "shop:shopCategory:info")),
    SHOP_CATEGORY_SAVE(Permission.of(10903, "新增", "shop:shopCategory:save")),
    SHOP_CATEGORY_UPDATE(Permission.of(10904, "编辑", "shop:shopCategory:update")),
    SHOP_CATEGORY_DELETE(Permission.of(10905, "删除", "shop:shopCategory:delete")),

    // 品牌店铺
    BRAND(Permission.of(11000, "品牌店铺", "shop:brand")),
    BRAND_LIST(Permission.of(11001, "列表", "shop:brand:page")),
    BRAND_INFO(Permission.of(11002, "详情", "shop:brand:info")),
    BRAND_SAVE(Permission.of(11003, "新增", "shop:brand:save")),
    BRAND_UPDATE(Permission.of(11004, "编辑", "shop:brand:update")),
    BRAND_DELETE(Permission.of(11005, "删除", "shop:brand:delete")),

    // 楼层管理
    FLOOR(Permission.of(11100, "楼层管理", "shop:floor")),

    // 区域
    MALL_AREA(Permission.of(11200, "区域管理", "shop:floor:mallArea")),
    MALL_AREA_LIST(Permission.of(11201, "列表", "shop:floor:mallArea:page")),
    MALL_AREA_INFO(Permission.of(11202, "详情", "shop:floor:mallArea:info")),
    MALL_AREA_SAVE(Permission.of(11203, "新增", "shop:floor:mallArea:save")),
    MALL_AREA_UPDATE(Permission.of(11204, "编辑", "shop:floor:mallArea:update")),
    MALL_AREA_DELETE(Permission.of(11205, "删除", "shop:floor:mallArea:delete")),

    // 楼层
    FLOOR_FLOOR(Permission.of(11300, "楼层管理", "shop:floor:floor")),
    FLOOR_LIST(Permission.of(11301, "列表", "shop:floor:floor:page")),
    FLOOR_INFO(Permission.of(11302, "详情", "shop:floor:floor:info")),
    FLOOR_SAVE(Permission.of(11303, "新增", "shop:floor:floor:save")),
    FLOOR_UPDATE(Permission.of(11304, "编辑", "shop:floor:floor:update")),
    FLOOR_DELETE(Permission.of(11305, "删除", "shop:floor:floor:delete")),

    // 优惠券
    COUPON(Permission.of(11400, "优惠券", "shop:coupon")),
    COUPON_LIST(Permission.of(11401, "列表", "shop:coupon:page")),
    COUPON_SAVE(Permission.of(11402, "新增", "shop:coupon:save")),
    COUPON_UPDATE(Permission.of(11403, "编辑", "shop:coupon:update")),
    COUPON_DELETE(Permission.of(11404, "删除", "shop:coupon:delete")),
    COUPON_SHELF(Permission.of(11405, "上下架", "shop:coupon:shelf")),
    COUPON_STOCK(Permission.of(11406, "修改库存", "shop:coupon:stock")),

    // 核销
    WRITE_OFF(Permission.of(11500, "核销", "shop:writeOff")),
    WRITE_OFF_LIST(Permission.of(11501, "列表", "shop:writeOff:page")),

    // 商户意见反馈管理
    FEEDBACK(Permission.of(11600, "商户意见反馈管理", "shop:feedback")),
    FEEDBACK_LIST(Permission.of(11601, "列表", "shop:feedback:page")),
    FEEDBACK_INFO(Permission.of(11602, "详情", "shop:feedback:info")),
    FEEDBACK_SAVE(Permission.of(11603, "新增", "shop:feedback:save")),
    FEEDBACK_UPDATE(Permission.of(11604, "编辑", "shop:feedback:update")),
    FEEDBACK_DELETE(Permission.of(11605, "删除", "shop:feedback:delete")),

    // 商户报表
    MERCHANT_REPORT(Permission.of(11700, "商户报表", "shop:merchantReport")),
    MERCHANT_REPORT_LIST(Permission.of(11701, "列表", "shop:merchantReport:page")),
    MERCHANT_REPORT_INFO(Permission.of(11702, "详情", "shop:merchantReport:info")),
    MERCHANT_REPORT_SAVE(Permission.of(11703, "新增", "shop:merchantReport:save")),
    MERCHANT_REPORT_UPDATE(Permission.of(11704, "编辑", "shop:merchantReport:update")),
    MERCHANT_REPORT_DELETE(Permission.of(11705, "删除", "shop:merchantReport:delete")),

    // 小票
    RECEIPT(Permission.of(11800, "小票", "receipt")),
    RECEIPT_LIST(Permission.of(11801, "列表", "receipt:page")),
    RECEIPT_AUDIT(Permission.of(11802, "审核", "receipt:audit")),

    // 优惠券分类
    COUPON_CATEGORY(Permission.of(11900, "优惠券分类", "couponCategory")),
    COUPON_CATEGORY_LIST(Permission.of(11901, "列表", "couponCategory:list")),
    COUPON_CATEGORY_SAVE(Permission.of(11902, "新增", "couponCategory:save")),
    COUPON_CATEGORY_UPDATE(Permission.of(11903, "编辑", "couponCategory:update")),
    COUPON_CATEGORY_DELETE(Permission.of(11904, "删除", "couponCategory:delete")),

    // 活动管理
    ACTIVITY(Permission.of(12000, "活动管理", "activity")),
    ACTIVITY_LIST(Permission.of(12001, "列表", "activity:page")),
    ACTIVITY_INFO(Permission.of(12002, "详情", "activity:info")),
    ACTIVITY_SAVE(Permission.of(12003, "新增", "activity:save")),
    ACTIVITY_UPDATE(Permission.of(12004, "编辑", "activity:update")),
    ACTIVITY_DELETE(Permission.of(12005, "删除", "activity:delete")),
    ACTIVITY_EXPORT(Permission.of(12006, "导出", "activity:export")),

    // 社区服务
    COMMUNITY(Permission.of(12100, "社区服务", "community")),
    COMMUNITY_LIST(Permission.of(12101, "列表", "community:page")),
    COMMUNITY_INFO(Permission.of(12102, "详情", "community:info")),
    COMMUNITY_SAVE(Permission.of(12103, "新增", "community:save")),
    COMMUNITY_UPDATE(Permission.of(12104, "编辑", "community:update")),
    COMMUNITY_DELETE(Permission.of(12105, "删除", "community:delete")),

    // 场地管理
    VENUE(Permission.of(12200, "场地管理", "venue")),
    VENUE_LIST(Permission.of(12201, "列表", "venue:page")),
    VENUE_INFO(Permission.of(12202, "详情", "venue:info")),
    VENUE_SAVE(Permission.of(12203, "新增", "venue:save")),
    VENUE_UPDATE(Permission.of(12204, "编辑", "venue:update")),
    VENUE_DELETE(Permission.of(12205, "删除", "venue:delete")),

    // 车辆管理
    CAR(Permission.of(12300, "车辆管理", "car")),
    CAR_LIST(Permission.of(12301, "列表", "car:page")),

    // 报修信息管理
    REPAIR(Permission.of(12400, "报修信息管理", "repair")),
    REPAIR_LIST(Permission.of(12401, "列表", "repair:page")),
    REPAIR_INFO(Permission.of(12402, "详情", "repair:info")),
    REPAIR_SAVE(Permission.of(12403, "新增", "repair:save")),
    REPAIR_UPDATE(Permission.of(12404, "编辑", "repair:update")),
    REPAIR_DELETE(Permission.of(12405, "删除", "repair:delete")),

    // 用户优惠券
    USER_COUPON(Permission.of(12500, "用户优惠券", "userCoupon")),
    USER_COUPON_LIST(Permission.of(12501, "列表", "userCoupon:page")),
    USER_COUPON_EXPORT(Permission.of(12502, "导出", "userCoupon:export")),
    USER_COUPON_REFUND_AUDIT(Permission.of(12503, "退款审核", "userCoupon:refundAudit")),

    // banner管理
    BANNER(Permission.of(12600, "banner管理", "banner")),
    BANNER_LIST(Permission.of(12601, "列表", "banner:page")),
    BANNER_INFO(Permission.of(12602, "详情", "banner:info")),
    BANNER_SAVE(Permission.of(12603, "新增", "banner:save")),
    BANNER_UPDATE(Permission.of(12604, "编辑", "banner:update")),
    BANNER_DELETE(Permission.of(12605, "删除", "banner:delete")),

    // 配置
    CONFIG(Permission.of(12700, "配置管理", "config")),
    CONFIG_CREDIT_RATE(Permission.of(12701, "积分比率", "config:creditRate")),
    CONFIG_CREDIT_RATE_UPDATE(Permission.of(12702, "积分比率编辑", "config:creditRate:update")),
    CONFIG_REGISTER_CREDIT(Permission.of(12703, "首次注册获得积分", "config:registerCredit")),
    CONFIG_REGISTER_CREDIT_UPDATE(Permission.of(12704, "首次注册获得积分编辑", "config:registerCredit:update")),
    CONFIG_PARK_TIME_RATE(Permission.of(12705, "积分兑换停车时长", "config:parkTimeRate")),
    CONFIG_PARK_TIME_RATE_UPDATE(Permission.of(12706, "积分兑换停车时长编辑", "config:parkTimeRate:update")),
    CONFIG_PARK_AMOUNT_RATE(Permission.of(12707, "积分兑换停车金额", "config:parkAmountRate")),
    CONFIG_PARK_AMOUNT_RATE_UPDATE(Permission.of(12708, "积分兑换停车金额编辑", "config:parkAmountRate:update")),
    CONFIG_FREE_PARK_TIME(Permission.of(12709, "免费停车时长", "config:freeParkTime")),
    CONFIG_FREE_PARK_TIME_UPDATE(Permission.of(12710, "免费停车时长编辑", "config:freeParkTime:update")),

    // 报修单问题分类
    // REPAIR_PROBLEM_CATEGORY(Permission.of(12800, "报修单问题分类", "repairProblemCategory")),
    // REPAIR_PROBLEM_CATEGORY_LIST(Permission.of(12801, "列表", "repairProblemCategory:page")),
    // REPAIR_PROBLEM_CATEGORY_INFO(Permission.of(12802, "详情", "repairProblemCategory:info")),
    // REPAIR_PROBLEM_CATEGORY_SAVE(Permission.of(12803, "新增", "repairProblemCategory:save")),
    // REPAIR_PROBLEM_CATEGORY_UPDATE(Permission.of(12804, "编辑", "repairProblemCategory:update")),
    // REPAIR_PROBLEM_CATEGORY_DELETE(Permission.of(12805, "删除", "repairProblemCategory:delete"));

    // 停车订单
    CAR_PARK_ORDER(Permission.of(12900, "停车订单", "carParkOrder")),
    CAR_PARK_ORDER_LIST(Permission.of(12901, "列表", "carParkOrder:page")),
    CAR_PARK_ORDER_INFO(Permission.of(12902, "详情", "carParkOrder:info")),
    CAR_PARK_ORDER_EXPORT(Permission.of(12903, "导出", "carParkOrder:export")),

    //活动回顾
    ACTIVITY_REVIEW(Permission.of(13000, "活动回顾", "activityReview")),
    ACTIVITY_REVIEW_LIST(Permission.of(13001, "列表", "activityReview:page")),
    ACTIVITY_REVIEW_INFO(Permission.of(13002, "详情", "activityReview:info")),
    ACTIVITY_REVIEW_SAVE(Permission.of(13003, "新增", "activityReview:save")),
    ACTIVITY_REVIEW_UPDATE(Permission.of(13004, "编辑", "activityReview:update")),
    ACTIVITY_REVIEW_DELETE(Permission.of(13005, "删除", "activityReview:delete")),
    ;




    private static final Map<Integer, Permission> ID_MAP = new HashMap<>();
    private static final List<Permission> PERMISSION_TREE;

    private final Permission permission;

    static {
        Map<String, Permission> identifierMap = new HashMap<>();
        Map<Integer, List<Permission>> levelMap = new TreeMap<>();
        for (EPermission ePermission : values()) {
            Permission permission = ePermission.getPermission();
            Permission previous = ID_MAP.put(permission.getId(), permission);
            if (previous != null) {
                throw new RuntimeException("权限id存在重复值，请检查");
            }

            Permission permissionX = Permission.of(permission);
            identifierMap.put(permissionX.getIdentifier(), permissionX);
            List<Permission> permissions = levelMap.computeIfAbsent(permissionX.getIdentifier().split(":").length, k -> new ArrayList<>());
            permissions.add(permissionX);
        }
        Map<String, Set<String>> codeMap = new LinkedHashMap<>();
        for (Integer level : levelMap.keySet()) {
            List<Permission> permissions = levelMap.get(level);
            for (Permission permission : permissions) {
                String identifier = permission.getIdentifier();
                String[] identifierCodes = identifier.split(":");
                for (int i = identifierCodes.length - 2; i >= 0; i--) {
                    String combinedCode = IntStream.rangeClosed(0, i).mapToObj(k -> identifierCodes[k]).collect(Collectors.joining(":"));
                    Set<String> childSet = codeMap.get(combinedCode);
                    if (childSet != null) {
                        childSet.add(identifier);
                        break;
                    }
                }
                codeMap.put(identifier, new LinkedHashSet<>());
            }
        }
        for (String code : codeMap.keySet()) {
            Permission permission = identifierMap.get(code);
            Set<String> childrenCodes = codeMap.get(code);
            permission.setChildren(childrenCodes.stream().map(identifierMap::get).collect(Collectors.toCollection(LinkedHashSet::new)));
        }

        int topLevel = levelMap.keySet().iterator().next();
        PERMISSION_TREE = levelMap.get(topLevel);
    }

    public static Permission byId(int id) {
        return ID_MAP.get(id);
    }

    public static List<Permission> getPermissionTree() {
        return PERMISSION_TREE;
    }
}
